<!DOCTYPE html> 
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<meta charset="utf-8">
<title>账号设置</title>
<link href="jquery-mobile/jquery.mobile.min.css" rel="stylesheet" type="text/css"/>
<script src="jquery-mobile/jquery.min.js" type="text/javascript"></script>
<script src="jquery-mobile/jquery.mobile.min.js" type="text/javascript"></script>
<script src="js/cordova.js" type="text/javascript"></script>

<script type="text/javascript">
function back(){
	if (typeof(navigator) != 'undefined' && typeof(navigator.app) != 'undefined' && typeof(navigator.app.backHistory) == 'function') {
		navigator.app.backHistory();
	} else{
		history.back();
	}
}

$(document).ready(function(){
	jQuery('body').live('swipeleft',function(event){
		back();
		//event.preventDefault();
	});
	jQuery('body').live('swiperight',function(event){
		window.history.forward();
		//event.preventDefault();
	});
	$("#sinaframe").css('width', document.body.clientWidth);
	$("#sinaframe").css('height', document.body.clientHeight - $("#sinaframe").offset().top);
});

$('.ui-btn-back').live('tap',function() {
	back();
	return false;
}).live('click',function() {
	back();
	return false;
});


</script>

</head>
<body>
<div data-role="page">
	 <div data-role="header"  data-theme="b">
    <a href="#" class="ui-btn-back">返回</a>
    <h1>新浪授权</h1>
   </div>
   <div date-role="content">
     <iframe id="sinaframe" scrolling="yes" src="https://api.weibo.com/oauth2/authorize?client_id=3943049038&redirect_uri=http://ihavecar.sinaapp.com/callback.php&response_type=code&display=mobile">
     </iframe>
     </iframe>
   </div>
</div>

<script type="text/javascript">
var access_token;
var uid;
var image;
var nickname;
var expires_time;


// iframe内的数据与本地phonegap数据跨域通讯
window.onmessage=function(e){
	var json_str = e.data;
	 access_token = json_str.access_token;
	 uid = json_str.uid;
	 image = json_str.image;
	 nickname = json_str.name;
	 expires_time= json_str.current_time+json_str.expires_in;
	// alert('before post');
	 
//用户数据上传至SAE数据库，不传输access_token，必须在success后写文件
	$.ajax({type:'POST',async:false, url:'http://ihavecar.sinaapp.com/postuser.php?nickname='+nickname+'&photo='+image+'&sina='+uid+'&uid='+uid+'&qq=0'}, writefile() );
	//alert('post ok');

	
function writefile(){
	//等待加载PhoneGap
	document.addEventListener("deviceready", onDeviceReady, false);

	// PhoneGap加载完毕
	function onDeviceReady() {
		//alert('device ready');
		window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
	}
         //获取newFile目录，如果不存在则创建该目录
	function gotFS(fileSystem) {
		console.log('got fs' + fileSystem.name);
		newFile = fileSystem.root.getDirectory("ucar", {create : true,exclusive : false}, writerFile, fail);	
	}
	//获取newFile目录下面的dataFile.txt文件，如果不存在则创建此文件
	function writerFile(newFile) {
		console.log('writer file');
		newFile.getFile("DataFile.txt", {create : true,exclusive : false}, gotFileEntry, fail);
	}
	
	function gotFileEntry(fileEntry) {
		console.log('get file entry');
		fileEntry.createWriter(gotFileWriter, fail);
	}

	function gotFileWriter(writer) {
		writer.onwrite = function(evt) {
			//返回登录页面,必须确保返回前已经写文件完成	
			window.location.href ='login.html';
			
		};
		writer.write("uid="+uid+";accesstoken="+access_token+";nickname="+nickname+";image="+image+";expires_time="+expires_time+";");
	}

	function fail(error) {
		alert('error');
		alert("Failed to write file:" + error.code);
	}
	// 检索一个已存在的文件，如果该文件不存在时则创建该文件
}	
	
	
	
};	
		
</script>
</body>
</html>